// 结构化搜索，精确匹配
DELETE products

// 这里批量插入得复制到kibana 中执行, vscode es插件执行不了
POST /products/_bulk
{ "index": { "_id": 1 }}
{ "price" : 10,"avaliable":true,"date":"2018-01-01", "productID" : "XHDK-A-1293-#fJ3", "genre":["Comedy","Romance"], "genreCnt": 2 }
{ "index": { "_id": 2 }}
{ "price" : 20,"avaliable":true,"date":"2019-01-01", "productID" : "KDKE-B-9947-#kL5", "genre":"Comedy", "genreCnt": 1}
{ "index": { "_id": 3 }}
{ "price" : 30,"avaliable":true, "productID" : "JODL-X-1937-#pV7" }
{ "index": { "_id": 4 }}
{ "price" : 30,"avaliable":false, "productID" : "QQPX-R-3956-#aD8" }

GET products/_mapping

// 对布尔值 match 查询, 有算分; profile.query.type: TermQuery
POST products/_search
{
  "profile": "true",
  "explain": true,
  "query": {
    "term": {
      "avaliable": true
    }
  }
}

// 对布尔值，通过constant score 转成 filtering, 没有算分, 分数固定为1; profile.query.type: ConstantScoreQuery.ConstantScore
POST products/_search
{
  "profile": "true",
  "explain": true,
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "avaliable": true
        }
      }
    }
  }
}

// filter 也是 filter context, 分数固定为0; profile.query.type: BoostQuery.ConstantScore
POST products/_search
{
    "profile": "true",
    "explain": true,
    "query": {
        "bool": {
            "filter": {
                "term": {
                    "avaliable": true
                }
            }
        }
    }
}

// 数字类型term; profile.query.type: PointRangeQuery
POST products/_search
{
    "profile": "true",
    "explain": true,
    "query": {
        "term": {
            "price": 30
        }
    }
}

// profile.query.type: ConstantScoreQuery.ConstantScore
POST products/_search
{
    "profile": "true",
    "query": {
        "constant_score": {
            "filter": {
                "term": {
                    "price": 30
                }
            }
        }
    }
}

// 数字 Range 查询
GET products/_search
{
    "query": {
        "constant_score": {
            "filter": {
                "range": {
                    "price": {
                        "gte": 20,
                        "lte": 30
                    }
                }
            }
        }
    }
}

// 日期 range
POST products/_search
{
    "query": {
        "constant_score": {
            "filter": {
                "range": {
                    "date": {
                        "gte": "now-3y"
                    }
                }
            }
        }
    }
}

// exists查询
POST products/_search
{
    "query": {
        "constant_score": {
            "filter": {
                "exists": {
                    "field": "date"
                }
            }
        }
    }
}

// 处理多值字段, term查询时包含, 而不是完全相等
POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "genre.keyword": "Comedy"
        }
      }
    }
  }
}

// 处理多值字段, term查询需要完全相等, 在文档当中增加一个计数来实现
GET products/_search
{
    "query": {
        "bool": {
            "must": [
                {"term": {"genre.keyword": {"value": "Comedy"}}},
                {"term": {"genreCnt": {"value": 1}}}
            ]
        }
    }
}

GET products/_search
{
    "query": {
        "bool": {
            "filter": [
                {"term": {"genre.keyword": {"value": "Comedy"}}},
                {"term": {"genreCnt": {"value": 1}}}
            ]
        }
    }
}


DELETE products